<template>
  <div>
    <Header v-if="shows == 'list'">
      <el-button size="small" type="primary" @click="add">
        新建wifi
      </el-button>
    </Header>
    <Main v-if="shows == 'list'" :data="listData" :footer-lock="totals > 10">
      <el-table-column
        label="wifi名称"
        align="center"
        property="wifi_name"
      ></el-table-column>
      <el-table-column
        label="SSID"
        align="center"
        property="ssid"
      ></el-table-column>
      <el-table-column
        label="BSSID"
        align="center"
        property="bssid"
      ></el-table-column>
      <el-table-column
        label="wifi密码"
        align="center"
        property="passwd"
      ></el-table-column>
      <!-- <el-table-column label='可用状态'
                               align='center'
                               property='title'></el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="250"
        class-name="tableoperation"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="del(scope.row.wifi_id)">
            删除
          </el-button>
        </template>
      </el-table-column>

      <Footer :total="totals">
        <el-pagination
          :page-size="10"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </Footer>
    </Main>
    <Add v-if="shows == 'add'" @out="out" />
  </div>
</template>
<script>
import { WIFI_LIST, WIFI_ADD } from "../service";

export default {
  components: {
    Add: () => import("./form")
  },
  data() {
    return {
      shows: "list",
      listData: [],
      totals: 0,
      pages: 1
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async del(id) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          WIFI_ADD({ wifi_id: id, type: "del" }).then(({ code, msg }) => {
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除!"
          });
        });
    },
    async getList() {
      await WIFI_LIST({
        page: this.pages,
        pageshow: 10
      }).then(({ code, count, data, msg }) => {
        if (code === 200) {
          this.listData = data;
          this.totals = count;
        } else {
          this.$message.error(msg);
          this.listData = [];
        }
      });
    },
    add() {
      this.shows = "add";
    },
    out() {
      this.shows = "list";
      this.wifi_id = "";
      this.getList();
    },
    changes(page) {
      this.pages = page;
      this.getList();
    }
  }
};
</script>
